<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2021/3/23
 * Time: 15:20
 */

namespace App\Admin\Selectable;

use App\Models\AdminUser;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Grid\Selectable;

class UserSelectTable extends Selectable
{
    public $model = AdminUser::class;

    public function make()
    {
        $user = Admin::user();
        $this->column('eno', '工号');
        $this->column('name', '姓名');
        $this->column('mobile', '电话');
        $this->column('department.name', '院系');
        $this->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->where(function ($query) {
                $query->where('eno', "{$this->input}")
                    ->orWhere('name', 'like', "%{$this->input}%")
                    ->orWhere('mobile', "{$this->input}");
            }, '关键字')->placeholder('工号/姓名/电话');
        });
        Admin::style($this->style());
    }

    private function style()
    {
        return <<<STYLE
     .grid-table .empty-grid { display: none !important; }
STYLE;
    }

    public static function selectScript($key)
    {
        $url = admin_route('api-user');
        return <<<EOT
 $("select[name='{$key}']").on('change', function () {
        console.log(111);
        var q = $("select[name='{$key}']").val();
        $.get('{$url}', {
            'q': q,
        }, function (data) {
            $("input[name='mobile']").val(data.mobile);
            $("input[name='email']").val(data.email);
        });
    });
EOT;

    }

}
